//
//  StatusOriginalView.swift
//  Weibo
//
//  Created by 周先生 on 15/11/15.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class StatusOriginalView: UIView {

    
    //原创微博底部约束 用来记录底部约束 后续根据是否有配图 来动态修改
    var bottomConstraints: Constraint?

    //数据模型
    var status: Status? {
        didSet {
        
            iconImage.sd_setImageWithURL(status?.user?.headImageUrl, placeholderImage: UIImage(named: "avatar_default_big"))
            namelLabel.text = status?.user?.name
            memberIconView.image = status?.user?.mbrankImage
            verifiedImage.image = status?.user?.verified_type_Image
            timeLabel.text = "22:22" //status?.created_at
            sourceLabel.text = "main微博"  //status?.source
            contentLabel.text = status?.text
            
            //移除某个约束
            self.bottomConstraints?.uninstall()
            
            
            //给配图视图设置数据源
            if let picturls = status?.imageUrls where picturls.count > 0 {
                //有视图配图
                pictureView.imageUrls = status?.imageUrls
                //显示配图视图
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件装换成约束类型
                    self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            } else {
            
                //没有配图视图
                //跟新约束
                //隐藏配图视图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    //将约束条件转换成约束类型
                    self.bottomConstraints = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            
            }
            
            
        }
    
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //调用
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    private func setupUI() {
    
//        backgroundColor = UIColor(white: 0.93, alpha: 1)
        //添加子控件
        sepView.backgroundColor = UIColor(white: 0.9, alpha: 1)
        addSubview(sepView)
        addSubview(iconImage)
        addSubview(namelLabel)
        addSubview(verifiedImage)
        addSubview(memberIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        iconImage.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(sepView.snp_top).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.height.equalTo(StatusCellIconWidth)
            make.width.equalTo(StatusCellIconWidth)
        }
        namelLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_top)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            
        }
        verifiedImage.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(iconImage.snp_right)
            make.centerX.equalTo(iconImage.snp_bottom)
            
        }
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(namelLabel.snp_top)
            make.left.equalTo(namelLabel.snp_right).offset(StatusCellMargin)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImage.snp_bottom)
            make.left.equalTo(iconImage.snp_right).offset(StatusCellMargin)
            
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(timeLabel.snp_bottom)
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImage)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentLabel.snp_left)
//            make.height.equalTo(100)
//            make.width.equalTo(100)
        }

            /// 原创微博的底部相对于微博正文的底部对齐
        self.snp_makeConstraints { (make) -> Void in
            //将约束类型转换成 约束类型
          self.bottomConstraints = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
    
    }
    
    //懒加载控件
    
    private lazy var sepView: UIView = UIView()
    //头像
    private lazy var iconImage: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //昵称
    private lazy var namelLabel: UILabel = UILabel(title: "championzhou", size: 14, color: UIColor.darkGrayColor())
    //用户认证类型vip
    private lazy var verifiedImage: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //用户等级
    private lazy var memberIconView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    //发布时间
    private lazy var timeLabel: UILabel = UILabel(title: "11:22", size: 10, color: UIColor.orangeColor())
    //微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "五行之外", size: 10, color: UIColor.lightGrayColor())
    //微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "人生如只如初见,何事秋风悲画扇", size: 14, color: UIColor.lightGrayColor(), margin: StatusCellMargin)
    //图片
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    
    
}
